package com.superatom.springframework.beans.factory.parsing;

import com.superatom.springframework.lang.Nullable;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author Kwokchiu Wong
 * @Title:
 * @Package
 * @Description:
 * @date 2020/10/19 10:34 上午
 */
public class FailFastProblemReporter implements ProblemReporter {

    private Log logger = LogFactory.getLog(getClass());


    /**
     * Set the {@link Log logger} that is to be used to report warnings.
     * <p>If set to {@code null} then a default {@link Log logger} set to
     * the name of the instance class will be used.
     * @param logger the {@link Log logger} that is to be used to report warnings
     */
    public void setLogger(@Nullable Log logger) {
        this.logger = (logger != null ? logger : LogFactory.getLog(getClass()));
    }


    /**
     * Throws a {@link BeanDefinitionParsingException} detailing the error
     * that has occurred.
     * @param problem the source of the error
     */
    @Override
    public void fatal(Problem problem) {
        throw new BeanDefinitionParsingException(problem);
    }

    /**
     * Throws a {@link BeanDefinitionParsingException} detailing the error
     * that has occurred.
     * @param problem the source of the error
     */
    @Override
    public void error(Problem problem) {
        throw new BeanDefinitionParsingException(problem);
    }

    /**
     * Writes the supplied {@link Problem} to the {@link Log} at {@code WARN} level.
     * @param problem the source of the warning
     */
    @Override
    public void warning(Problem problem) {
        logger.warn(problem, problem.getRootCause());
    }

}
